package com.xintoucloud.pbs.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.xintoucloud.pbs.dao.PartyMemberDao;
import com.xintoucloud.pbs.entity.PartyMember;

@Repository@Transactional
public class PartyMemberDaoImpl implements PartyMemberDao{
    @Autowired
	private SessionFactory sessionFactory;
	@Override
	public PartyMember getOne(Integer id) {
		return sessionFactory.getCurrentSession().get(PartyMember.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PartyMember> list() {
		System.out.println("0000000000000000000000000000");
		return sessionFactory.getCurrentSession().createQuery("from PartyMember").getResultList();
	}

	@Override
	public Serializable save(PartyMember partyMember) {
		return sessionFactory.getCurrentSession().save(partyMember);
	}

	@Override
	public void delete(int id) {
		sessionFactory.getCurrentSession().delete(getOne(id));
	}

	@Override@Transactional(readOnly=true)
	public PartyMember login(String pname) {
		return  (PartyMember) sessionFactory.getCurrentSession().createQuery("from PartyMember where pname=?0").setParameter(0, pname).uniqueResult();
	}

	@Override
	public void update(PartyMember partyMember) {
		int pid=partyMember.getPid();
		String email=partyMember.getEmail();
		String idnum=partyMember.getIdnum();
		String pname=partyMember.getPname();
		String rname=partyMember.getRname();
		String phone=partyMember.getPhone();
		String sex=partyMember.getSex();
		int sid=partyMember.getPms().getSid();
		System.out.println("sid:"+sid);
		sessionFactory.getCurrentSession().createQuery("update PartyMember p set p.email ='"+email+"',p.idnum='"+idnum+"',p.pname='"+pname+"',p.rname='"+rname+"',p.phone='"+phone+"',p.sex='"+sex+"',p.pms.sid="+sid+" where pid = "+pid+"").executeUpdate();
		//sessionFactory.getCurrentSession().createQuery("update PartyMember p set p.email =?0,p.idnum=?1,p.pname=?2,p.rname=?3,p.phone=?4,p.sex=?5,p.oid=?6 where pid =?7 ").setParameter(0, email).setParameter(1, idnum).setParameter(2, pname).setParameter(3, rname).setParameter(4, phone).setParameter(5, sex).setParameter(6, oid).setParameter(7, pid).executeUpdate();
	}

	@Override
	public long count() {
		return  (long) sessionFactory.getCurrentSession().createQuery("select count(*) from PartyMember").uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PartyMember> list(int pageIndex, int rows) {
		return  sessionFactory.getCurrentSession().createCriteria(PartyMember.class).setFirstResult(pageIndex).setMaxResults(rows).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PartyMember> listByOid(int oid) {
		return sessionFactory.getCurrentSession().createQuery("from PartyMember where oid=?0").setParameter(0, oid).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	//党委
	public List<PartyMember> list2(int pageIndex, int rows) {
		System.out.println("2222222222222222222222222222222222222");
		return sessionFactory.getCurrentSession().createQuery("from PartyMember where ppid>=3").list();
	}

	@Override
	public List<PartyMember> list3(int pageIndex, int rows) {
		System.out.println("33333333333333333333333333333333333333");
		return sessionFactory.getCurrentSession().createQuery("from PartyMember where ppid>=4").list();
	}

	@Override
	public List<PartyMember> list4(int oid,int pageIndex, int rows) {
		System.out.println("4444444444444444444444444444444444444");
		return sessionFactory.getCurrentSession().createQuery("from PartyMember where ppid>=5 and oid=?0").setParameter(0, oid).list();
	}
}
